//
//  XCHotListTableViewCell.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/8.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCHotListTableViewCell.h"
#import "XCHotListCollectionViewCell.h"
#import "XCHotSongListViewController.h"
#import "XCHotListCollectionViewCell.h"
@implementation XCHotListTableViewCell

- (void)dealloc {
    
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    
    [_collectionView release];
    [_mutableArray release];
    [_flowLayout release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        if ([UIScreen mainScreen].bounds.size.width > 410) {
            _flowLayout.itemSize = CGSizeMake(self.bounds.size.width / 3 , self.bounds.size.width / 3 + 42);
            _flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
        } else {
            
            _flowLayout.itemSize = CGSizeMake(self.bounds.size.width / 3 - self.bounds.size.width / 35 , self.bounds.size.width / 3 + 42);
            _flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        }
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.minimumLineSpacing = 10;
        

    
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];

        self.collectionView.backgroundColor = [UIColor clearColor];
        self.collectionView.bounces = NO;
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        
        [self.collectionView registerClass:[XCHotListCollectionViewCell class] forCellWithReuseIdentifier:@"Identifier"];
        [self.collectionView reloadData];
        [_collectionView release];
        [_flowLayout release];
        
        [self addSubview:self.collectionView];
    }
    return self;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width / 3 * (self.mutableArray.count / 3) + 42 * (self.mutableArray.count / 3));
}
#pragma mark - collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mutableArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XCHotListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Identifier" forIndexPath:indexPath];
//    NSLog(@"%@", self.mutableArray);
    XCSecDataModel *secModel = self.mutableArray[indexPath.row];
    if (secModel.listenCount) {
        [cell.listenButton setImage:[[UIImage imageNamed:@"Image-erji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        cell.SONLabel.text = @"天籁之音";
    }
    cell.secModel = secModel;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

        [self.delegate sendWithIndex:indexPath.row WithRow:self.row WithSection:self.section];
//    NSLog(@"%ld", (long)self.section);

}
- (void)setMutableArray:(NSMutableArray *)mutableArray {
    
    if (_mutableArray!=mutableArray) {
        [_mutableArray release];
        _mutableArray = [mutableArray retain];
    }
    [self.collectionView reloadData];

}
- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
